home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / mslang / cp1 / llink1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  3.2 KB  |  79 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 05-12-93 (10:32)             Number: 18
  4. From: PETER SMITH                  Refer#: 109
  5.   To: STEPHAN GALT                  Recvd: NO  
  6. Subj: visibility                     Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8.  On 05-10-93 Stephan Galt wrote to All...
  9.  
  10.  SG> Also a question has come up about the actual records I
  11.  SG> want to include in the structures.  Basically each struct
  12.  SG> would contain one string and a pointer to the next struct.
  13.  SG> The strings( to be read in from a file ) are of different
  14.  SG> lengths.  Can these be accomidated, or must they be 'forced'
  15.  SG> to be a given maximum (for example by char string[50];)?
  16.  
  17. Here is some code to get you started, (Its TESTED). This way your strings can
  18. be of nearly any size without wasting space. I've included one of the struct
  19. definitions. This is from an indexed dictionary, which used a link list for
  20. storage (sounds similar to what you are doing).
  21. I used my own function "selfError" but you can simply use a puts(); instead.
  22.  
  23. // Function to allocate space for a link in a link list.
  24. // by Peter Smith 1993
  25. // Donated to the Public Domain
  26.  
  27. typedef struct alink{
  28.     char *key;
  29.     char *value;
  30.     struct alink *next;
  31.     }ALINK;     // template for one link of linked list and define as a type
  32.  
  33. typedef ALINK *List;      // define type as ptr to ALINK
  34.  
  35. List makeSpaceForEntries(string akey, string value)
  36. {   // Private - allocate space for a new link and store the key/value pair
  37.     // in it and return pointer to it. Copies the value (and key when new)
  38.     // into heap space as simply storing the pointer does not work due to
  39.     // the object being pointed could be on the stack and may disappear.
  40.     // operation is expensive.
  41.     List alink;
  42.  
  43.  
  44.     alink = (List) malloc( sizeof (ALINK) );
  45.     if(alink == NULL)
  46.     {
  47.         selfError("unable to allocate more memory for dictionary");
  48.         exit(1);
  49.     }
  50.                                         // next allocate space for input string
  51.     alink->value = (char *) malloc(strlen(value) + 1);
  52.     if(alink->value == NULL)
  53.     {
  54.         selfError("Unable to allocate more memory for value");
  55.         exit(1);
  56.     }
  57.     strcpy(alink->value, value);        // copy string into space
  58.                                         // next allocate space for key input
  59.     alink->key = (char *) malloc(strlen(akey) + 1);
  60.     if(alink->key == NULL)
  61.     {
  62.         selfError("Unable to allocate more memory for key");
  63.         exit(1);
  64.     }
  65.     strcpy(alink->key, akey);           // copy string into space
  66.     alink->next = NULL;                 // as this is the last one in the list
  67.     return(alink);
  68. }
  69.  
  70. // end of code (as if you didn't know <g>!) ..........Pete
  71.  
  72. ... OFFLINE 1.50  "God does not speak out of a gun barrel."
  73.  
  74. --- Maximus 2.01wb
  75.  * Origin: Ned's Opus * Northern FDN Connection * (1:243/15)
  76. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  77. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 261/1023
  78. SEEN-BY: 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  79.